﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.Data;
using System.IO;

public partial class Controls_Activity_ImageDetail : System.Web.UI.UserControl
{
    private int m_activityId, m_imageId;
    private Photo m_curPhoto;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["activityid"]) && !string.IsNullOrEmpty(Request.QueryString["imageid"]))
        {
            m_activityId = Convert.ToInt32(Request.QueryString["activityid"]);
            m_imageId = Convert.ToInt32(Request.QueryString["imageid"]);
            DataTable dt_ImageInfo = PhotoRules.Instance.GetPhotosIndex(m_activityId, m_imageId);
            lb_CurIndex.Text = dt_ImageInfo.Rows[0]["CurIndex"].ToString();
            lb_Count.Text = dt_ImageInfo.Rows[0]["Count"].ToString();
            lb_Des.Text = dt_ImageInfo.Rows[0]["Description"].ToString();
            //hl_ImageListDetail.Attributes.Add("onmouseover", "javascript:tip_over();");
            //hl_ImageListDetail.Attributes.Add("onmouseout", "javascript:tip_overend();");
            //hl_ImageListDetail.Attributes.Add("onmousedown", "javascript:tip_overend();");
            if (Convert.ToInt32(dt_ImageInfo.Rows[0]["Count"]) == 0)
            {
                Response.Redirect("~/Activity/detail.aspx?activityid=" + m_activityId);
            }
            else if (Convert.ToInt32(dt_ImageInfo.Rows[0]["Count"]) == 1)
            {
                hl_PrevPhoto.Enabled = false;
                hl_NextPhoto.Enabled = false;
                hl_ShowNext.Enabled=false;
            }
            else
            {
                hl_PrevPhoto.NavigateUrl = "~/Activity/ImageDetail.aspx?activityid=" + m_activityId + "&imageid=" + dt_ImageInfo.Rows[0]["PrePhotoId"].ToString();
                hl_NextPhoto.NavigateUrl = "~/Activity/ImageDetail.aspx?activityid=" + m_activityId + "&imageid=" + dt_ImageInfo.Rows[0]["NextPhotoId"].ToString();
                hl_ShowNext.NavigateUrl = hl_NextPhoto.NavigateUrl;
                hl_ShowNext.ToolTip = "点击查看下一张";
            }
            m_curPhoto = PhotoRules.Instance.GetPhotoInfoById(m_imageId);
            img_Photo.ImageUrl = PhotoRules.Instance.GetActivityMiddlePhotoUrl(m_activityId, m_curPhoto.FileName);
            lb_CreateDateTime.Text = "上传于&nbsp;" + m_curPhoto.UploadDateTime.Value.ToString("yyyy年MM月dd日 HH:mm:ss");
            hl_ImageListDetail.NavigateUrl = "~/Activity/ImageListDetail.aspx?activityid=" + m_activityId;
            if (ActivityRules.Instance.IsActivityPromoter(m_activityId, UserRules.Instance.GetCurrentUserId()) || m_curPhoto.UserId == UserRules.Instance.GetCurrentUserId())
            {
                pn_Edit.Visible = true;
                lb_DeletePhoto.Click += new EventHandler(lb_DeletePhoto_Click);
                lb_DeletePhoto.OnClientClick = "return confirm('确定要删除这张照片么？')";
                hl_EditDes.NavigateUrl = "~/Activity/ImageEdit.aspx?activityid=" + m_activityId + "&imageid=" + m_imageId;
            }
            else
            {
                pn_Edit.Visible = false;
            }
            
        }
    }

    void lb_DeletePhoto_Click(object sender, EventArgs e)
    {
            PhotoRules.Instance.DeleteActivityPhoto(m_activityId,m_imageId);
            string bpath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\back\\");
            string spath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\s\\");
            string mpath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\m\\");
            if (File.Exists(bpath + m_curPhoto.FileName))
            {
                File.Delete(bpath + m_curPhoto.FileName);
            }
            if (File.Exists(spath + m_curPhoto.FileName))
            {
                File.Delete(spath + m_curPhoto.FileName);
            }
            if (File.Exists(mpath + m_curPhoto.FileName))
            {
                File.Delete(mpath + m_curPhoto.FileName);
            }
        if(lb_Count.Text=="1")
        {
            Response.Redirect("~/Activity/ImageListDetail.aspx?activityid=" + m_activityId);
        }
        else
        {
            Response.Redirect(hl_NextPhoto.NavigateUrl);
        }
    }
}
